#include <stdio.h>

#define LAND_GRAPHICS_CORE

#include "init.h"

static struct {
	LandGraphics* config;
	LandDrawBackend* backend;
	unsigned count;
}ctx = { 0 };

int
landInitDrawContext(LandGraphics* config, LandDrawBackend* backend) {
	ctx.config = config;
	ctx.backend = backend;
	return 0;
}

LAND_GRAPHICS_API void
landDrawPoints(LandCanvas* canvas, LandFloat size, LandDraw* draw) {
	ctx.backend->drawPoints(canvas, size, draw);
}

LAND_GRAPHICS_API void
landDraw(LandCanvas* canvas, LandDraw* draw) {
	ctx.count++;
	ctx.backend->draw(canvas, draw);
}

LAND_GRAPHICS_API void
landBeginDraw() {
	ctx.count = 0;
}

LAND_GRAPHICS_API unsigned
landCountDraw() {
	return ctx.count;
}

LAND_GRAPHICS_API unsigned
landRegUniformF(LandDraw* draw, const char* name, LandFloat f) {
	int index = draw->uniformCount++;
	LandUniform* u = &draw->uniforms[index];
	u->f = f;
	u->type = LAND_GRAPHICS_TFLOAT;
	sprintf_s(u->name, 128, "%s", name);
	return index;
}

LAND_GRAPHICS_API unsigned
landRegUniformV(LandDraw* draw, const char* name, LandVector* v) {
	int index = draw->uniformCount++;
	LandUniform* u = &draw->uniforms[index];
	u->v = *v;
	u->type = LAND_GRAPHICS_TVECTOR;
	sprintf_s(u->name, 128, "%s", name);
	return index;
}

LAND_GRAPHICS_API unsigned
landRegUniformM(LandDraw* draw, const char* name, LandMatrix* m) {
	int index = draw->uniformCount++;
	LandUniform* u = &draw->uniforms[index];
	u->m = *m;
	u->type = LAND_GRAPHICS_TMATRIX;
	sprintf_s(u->name, 128, "%s", name);
	return index;
}

LAND_GRAPHICS_API unsigned
landRegUniformTex(LandDraw* draw, const char* name, LandTexture* tex) {
	int index = draw->uniformCount++;
	LandUniform* u = &draw->uniforms[index];
	u->tex = tex;
	u->type = LAND_GRAPHICS_TTEXTURE;
	sprintf_s(u->name, 128, "%s", name);
	return index;
}

LAND_GRAPHICS_API unsigned
landRegUniformCanvas(LandDraw* draw, const char* name, LandCanvas* canvas) {
	int index = draw->uniformCount++;
	LandUniform* u = &draw->uniforms[index];
	u->canvas = canvas;
	u->type = LAND_GRAPHICS_TCANVAS;
	sprintf_s(u->name, 128, "%s", name);
	return index;
}

LAND_GRAPHICS_API void
landUniformF(LandDraw* draw, unsigned index, LandFloat f) {
	if (index >= draw->uniformCount) {
		return;
	}
	LandUniform* u = &draw->uniforms[index];
	if (u->type != LAND_GRAPHICS_TFLOAT) {
		return;
	}
	u->f = f;
}

LAND_GRAPHICS_API void
landUniformV(LandDraw* draw, unsigned index, LandVector* v) {
	if (index >= draw->uniformCount) {
		return;
	}
	LandUniform* u = &draw->uniforms[index];
	if (u->type != LAND_GRAPHICS_TVECTOR) {
		return;
	}
	u->v = *v;
}

LAND_GRAPHICS_API void
landUniformM(LandDraw* draw, unsigned index, LandMatrix* m) {
	if (index >= draw->uniformCount) {
		return;
	}
	LandUniform* u = &draw->uniforms[index];
	if (u->type != LAND_GRAPHICS_TMATRIX) {
		return;
	}
	u->m = *m;
}

LAND_GRAPHICS_API void
landUniformTex(LandDraw* draw, unsigned index, LandTexture* tex) {
	if (index >= draw->uniformCount) {
		return;
	}
	LandUniform* u = &draw->uniforms[index];
	if (u->type != LAND_GRAPHICS_TTEXTURE) {
		return;
	}
	u->tex = tex;
}

LAND_GRAPHICS_API void
landUniformCanvas(LandDraw* draw, unsigned index, LandCanvas* canvas) {
	if (index >= draw->uniformCount) {
		return;
	}
	LandUniform* u = &draw->uniforms[index];
	if (u->type != LAND_GRAPHICS_TCANVAS) {
		return;
	}
	if (u->canvas == canvas) {
		return;
	}
	u->canvas = canvas;
}

